"""Created the 2013-08-25 14:33 

@author:
"""
import sys

from libatomism import *
from PySide import QtCore, QtGui
from msWidget import *


class msZMatWidget(msWidget):
    """
    graphical component for msZMat
    """
    def __init__(self, objectdb, mainwindow):
        super(msZMatWidget, self).__init__(objectdb,mainwindow)

        self.zmatTextEdit = QtGui.QTextEdit()
        for line in self.data.getString().split('\n'):
            self.zmatTextEdit.append(line)

        hbox=QtGui.QHBoxLayout()
        vbox=QtGui.QVBoxLayout()
        vbox.addWidget( self.zmatTextEdit )
        self.loadFromCartBttn = QtGui.QPushButton("load from xyz")
        vbox.addWidget(self.loadFromCartBttn)
        hbox.addLayout(vbox)
        vbox=QtGui.QVBoxLayout()

        self.viewBttn = QtGui.QPushButton("watch")
        self.viewBttn.clicked.connect(self.watch)
        self.updateBttn = QtGui.QPushButton("update structure")
        self.updateBttn.clicked.connect(self.updateStructure)
        vbox.addWidget(self.updateBttn)

        vbox.addWidget(self.viewBttn)
        vbox.addStretch(1)
        hbox.addLayout(vbox)
        self.vbox.addLayout( hbox  ) 
        
        self.setMaximumSize(400,200)
        self.setMinimumSize(400,200)
  
    def updateStructure(self):

        text = str(self.zmatTextEdit.toPlainText()).replace('\n', ' ')
        try:
            self.data.set(text)
        except:
            self.mainwindows.reportException(sys.exc_info())

        self.mainwindows.clickTreeView()
   
    def watch(self):

        visu=msVisualizerZMat.New() 
        try:
            visu.launch(self.data)
        except:
            self.mainwindows.reportException(sys.exc_info())

        

   